<?php
/**
 * Created by PhpStorm.
 * User: thanatos
 * Date: 2018-11-29
 * Time: 17:00
 */

namespace App\Admin\Controllers;


use App\Http\Controllers\Controller;
use App\Models\GrabOrder;
use App\Models\User;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class GrabOrderController extends Controller
{

    /**
     * @param Content $content
     * @return Content
     * @author thanatos thanatos915@163.com
     */
    public function index(Content $content)
    {
        return $content
            ->header('抢票订单')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * @param $id
     * @param Content $content
     * @return Content
     * @author thanatos thanatos915@163.com
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('套餐')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * @param $id
     * @param Content $content
     * @return Content
     * @author thanatos thanatos915@163.com
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('套餐')
            ->description('修改')
            ->body($this->form()->edit($id));
    }

    protected function grid()
    {
        $grid = new Grid(new GrabOrder());

        $grid->id('ID')->sortable();
        $grid->columns([
            'order_no' => '订单号',
            'user.name' => '用户名',
            'from_station' => '始发站',
            'to_station' => '到达站',
            'start_train_date' => '出发日期',
            'mobile' => '电话',
            'user_12306' => '12306用户',
        ]);
        $grid->column('order_status', '订单状态')->display(function($status){
            switch ($status) {
                case 0:
                    return '未处理';
                case 1:
                    return '未支付';
                case 2:
                    return '购票成功';
                case 3:
                    return '已出票';
                case 4:
                    return '已退票';
                case 5:
                    return '已取消';
            }
        });
        $grid->created_at('创建时间');

        $grid->filter(function(Grid\Filter $filter){
            $filter->equal('order_status', '订单状态')->select([
                0 => '未处理', 1 =>'未支付', 2 => '购票成功', 3 => '已出票', 4 => '已退票', 5 => '已取消'
            ]);
        });
        $grid->disableCreateButton();
        $grid->actions(function(Grid\Displayers\Actions $actions) {
            $actions->disableEdit()->disableDelete();
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(GrabOrder::findOrFail($id));

        $show->id('ID');
        $show->fields([
            'order_no' => '订单号',
            'from_station' => '始发站',
            'to_station' => '到达站',
            'start_train_date' => '出发日期',
            'mobile' => '电话',
            'user_12306' => '12306用户',
        ]);
        $show->field('order_status', '订单状态')->as(function($status){
            switch ($status) {
                case 0:
                    return '未处理';
                case 1:
                    return '未支付';
                case 2:
                    return '购票成功';
                case 3:
                    return '已出票';
                case 4:
                    return '已退票';
                case 5:
                    return '已取消';
            }
        });
        $show->created_at('创建时间');
        $show->updated_at('修改时间');

        return $show;
    }

}